package com.ccrfid.rmc6.lbservice3.manager;

import com.ccrfid.rmc6.entity.asset.AssetEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelEntity;
import com.ccrfid.rmc6.entity.vehicle.VehicleEntity;
import com.ccrfid.rmc6.lbservice3.pojo.evt.EvtCardOffline;
import com.ccrfid.rmc6.lbservice3.pojo.locate.PersonnelLocateInfo;
import com.ccrfid.rmc6.lbservice3.pojo.msg.MsgLocate;
import com.ccrfid.rmc6.lbservice3.pojo.msg.MsgLocateActive;
import com.ccrfid.rmc6.pojo.Constants;
import com.ccrfid.rmc6.service.asset.AssetService;
import com.ccrfid.rmc6.service.device.CardService;
import com.ccrfid.rmc6.service.personnel.PersonnelService;
import com.ccrfid.rmc6.service.vehicle.VehicleService;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class MoveManager {
    private static final Logger logger = LoggerFactory.getLogger(MoveManager.class);
    @Autowired
    private EventBus eventBus;
    @Autowired
    private AssetService assetService;
    @Autowired
    private PersonnelService personnelService;
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private CardService cardService;
    @Value("${card.offline.mock.leave}")
    private boolean cardOfflineMockLeave;

    @PostConstruct
    public void init() {
        eventBus.register(this);
    }

    @Subscribe
    public void onMsgLocate(MsgLocate msgLocate) {
        long startTime = System.currentTimeMillis();
        //人员
        PersonnelEntity personnelEntity = personnelService.getPersonnelByCardNumber(msgLocate.getCardNumber());
        if (personnelEntity != null) {
            if (personnelEntity.getEnabled().equals(Constants.ENABLE_STATUS.ENABLE.id)) {
                PersonnelLocateInfo locateInfo = new PersonnelLocateInfo(personnelEntity.getId(),
                        personnelEntity.getPersonnelName(), personnelEntity.getPersonnelType(), msgLocate);
                logger.trace("post PersonnelMove, PersonnelLocateInfo: {}", locateInfo);
                eventBus.post(locateInfo);
                //记录历史轨迹
                personnelService.recordPersonnelMove(personnelEntity.getId(), msgLocate);
            } else {
                logger.trace("ignore personnel move - personnel is disabled, {}-{}",
                        personnelEntity.getCardNumber(), personnelEntity.getPersonnelName());
            }
        } else {
            //资产
            AssetEntity assetEntity = assetService.getAssetByCardNumber(msgLocate.getCardNumber());
            if (assetEntity != null) {
                if (assetEntity.getEnabled().equals(Constants.ENABLE_STATUS.ENABLE.id)) {
//                    AssetLocateInfo locateInfo = new AssetLocateInfo(assetEntity.getId(), msgLocate);
//                    logger.debug("post AssetMove, AssetLocateInfo: {}", locateInfo);
//                    eventBus.post(locateInfo);
                    //记录历史轨迹
                    assetService.recordAssetMove(assetEntity.getId(), msgLocate);
                } else {
                    logger.trace("ignore asset move - asset is disabled, {}-{}",
                            assetEntity.getCardNumber(), assetEntity.getAssetName());
                }
            } else {
                //车辆定位标签
                VehicleEntity vehicleEntity = vehicleService.getVehicleByCardNumber(msgLocate.getCardNumber());
                if (vehicleEntity != null) {
                    if (vehicleEntity.getEnabled().equals(Constants.ENABLE_STATUS.ENABLE.id)) {
                        //记录车辆历史轨迹
                        vehicleService.recordVehicleMove(vehicleEntity.getId(), msgLocate);
                    } else {
                        logger.trace("ignore vehicle move - vehicle is disabled, {}-{}",
                                vehicleEntity.getCardNumber(), vehicleEntity.getPlateNumber());
                    }
                }
            }
        }
        long stopTime = System.currentTimeMillis();
        if (logger.isTraceEnabled()) {
            logger.trace("onMsgLocate cost: {} ms, cardNumber: {}", stopTime - startTime, msgLocate.getCardNumber());
        }
    }

    //标签离线时，人员模拟离开
    @Subscribe
    public void onEvtCardOffline(EvtCardOffline evtCardOffline) {
        if (cardOfflineMockLeave) {
            MsgLocateActive msgLocateActive = new MsgLocateActive(evtCardOffline.getCardNumber(), evtCardOffline.getTime());
            msgLocateActive.setCurrentLocator(0);
            msgLocateActive.setX(null);
            msgLocateActive.setY(null);
            msgLocateActive.setAccuracy(null);
            msgLocateActive.setCurrentFloorNumber(null);
            //1.更新定位信息
            cardService.updateCardActive(msgLocateActive);

            MsgLocate msgLocate = new MsgLocate(evtCardOffline.getCardNumber(), evtCardOffline.getTime());
            msgLocate.setCurrentLocator(0);
            msgLocate.setCurrentPos(null);
            msgLocate.setCurrentFloorNumber(null);
            PersonnelLocateInfo locateInfo = new PersonnelLocateInfo(evtCardOffline.getPersonnelId(), msgLocate);
            eventBus.post(locateInfo);
            logger.debug("card {} offline, personnel {} mock leave", evtCardOffline.getCardNumber(), locateInfo.getPersonnelId());

        }
    }
}
